﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using WebBrowserTest;
using mshtml;

namespace easyForm
{
    public partial class Form1 : Form
    {
        public bool HasVerifycodel { get; set; }

        public string VerifyCode
        {
            get
            {
                return this.verifycodel.Text;
            }
        }

        public string QQ { get { return this.qqNumber.Text.Trim(); } }

        public string Pwd { get { return this.password.Text.Trim(); } }

        public Form1()
        {
            InitializeComponent();
        }

        private void openUrl_Click(object sender, EventArgs e)
        {
            this.HasVerifycodel = false;

            var uslStr = this.url.Text;

            if (uslStr.IsUrl())
            {
                this.webBrower.Url = new Uri(Url.Urlstr);
            }
            else
            {
                MessageBox.Show("url格式不正确请重新输入");
                this.url.Focus();
            }
        }

        private void setValue_Click(object sender, EventArgs e)
        {

            var document = this.webBrower.Document;
            var frames = document.Window.Frames;
            IHTMLDocument3 qqdoc = CorssDomainHelper.GetDocumentFromWindow(frames[0].DomWindow as IHTMLWindow2);

            var input = qqdoc.getElementById("u");

            input.click();
            if (string.IsNullOrEmpty(QQ))
            {
                MessageBox.Show("请输入QQ号");
                return;
            }
            if (string.IsNullOrEmpty(Pwd))
            {
                MessageBox.Show("请输入密码");
                return;
            }

            input.setAttribute("value", QQ, 0);
            var p = qqdoc.getElementById("p");
            p.click();
            
            p.setAttribute("value", Pwd, 0);
            Thread.Sleep(500);
            var vir = qqdoc.getElementById("verifyimg");
            if (vir != null)
            {
                this.HasVerifycodel = true;
                pictureBox1.Image = GetRegCodePic(this.webBrower.Document.Window.Frames[0].Document, "verifyimg", "", "");
            }


        }

        private void login_Click(object sender, EventArgs e)
        {

            var document = this.webBrower.Document;
            var frames = document.Window.Frames;
            IHTMLDocument3 qqdoc = CorssDomainHelper.GetDocumentFromWindow(frames[0].DomWindow as IHTMLWindow2);
            if (HasVerifycodel)
            {
                var verifycode = qqdoc.getElementById("verifycode");
                verifycode.click();
                //if (string.IsNullOrEmpty(VerifyCode))
                //{
                //    MessageBox.Show("请输入验证码");
                //    return;
                //}
                verifycode.setAttribute("value", this.verifycodel.Text);
            }
            qqdoc.getElementById("login_button").click();
            Thread.Sleep(4000);
            if (qqdoc.getElementById("login_button") != null)
            {
                var vir = qqdoc.getElementById("verifyimg");
                if (vir != null)
                {
                    this.HasVerifycodel = true;
                    pictureBox1.Image = GetRegCodePic(this.webBrower.Document.Window.Frames[0].Document, "verifyimg", "", "");
                }
            }

        }

        private void log_Click(object sender, EventArgs e)
        {
            var document = this.webBrower.Document;
            var log=document.GetElementById("OFP_LEFT_APP_Entry_blog");
            if(log==null){
                var lista=document.GetElementsByTagName("a");
                foreach (HtmlElement a in lista)
                {
                    if (a.GetAttribute("title") == "日志")
                    {
                        log = a;
                    }
                }
            }
            log.InvokeMember("click");
        }

        private void oplog_Click(object sender, EventArgs e)
        {
            var document = this.webBrower.Document;
            var frames = document.Window.Frames;
            IHTMLDocument3 qqdoc = CorssDomainHelper.GetDocumentFromWindow(frames[0].DomWindow as IHTMLWindow2);

            IHTMLElementCollection linkss = ((HTMLDocument)(qqdoc.documentElement.document)).links;
            foreach (mshtml.IHTMLElement element in linkss)
            {
                //加入你的代码就可以了。
                if (element.getAttribute("rel", 1) == null)
                    continue;
                string str = element.getAttribute("rel", 1).ToString().Trim();
                if (str == "write-blog-link")
                {
                    element.click();
                }
            }

        }

        private void fillTitle_Click(object sender, EventArgs e)
        {
            var document = this.webBrower.Document;
            var frames = document.Window.Frames;
            IHTMLDocument3 qqdoc = CorssDomainHelper.GetDocumentFromWindow(frames[0].DomWindow as IHTMLWindow2);

            var title = qqdoc.getElementById("blog-title-input");
            title.setAttribute("value", "123123", 1);


        }

        private void fillcontent_Click(object sender, EventArgs e)
        {
            var content = this.webBrower.Document.Window.Frames[0].Frames[0].Document.GetElementsByTagName("div")[0];
            content.SetAttribute("innerHtml", "333333");

        }

        private void submit_Click(object sender, EventArgs e)
        {
            this.webBrower.Document.Window.Frames[0].Document.GetElementById("saveBlogButton").InvokeMember("click");

        }

        public Image GetRegCodePic(HtmlDocument wbMail, string ImgName, string Src, string Alt)
        {
            HTMLDocument doc = (HTMLDocument)wbMail.DomDocument;
            HTMLBody body = (HTMLBody)doc.body;
            IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange();
            IHTMLControlElement Img;
            Img = (IHTMLControlElement)wbMail.All[ImgName].DomElement;
            rang.add(Img);
            rang.execCommand("Copy", false, null);
            Image RegImg = Clipboard.GetImage();
            Clipboard.Clear();
            return RegImg;
        }



    }
}
